package cn.xizikelif.org.service.impl;

import cn.xizikelif.basic.query.BaseQuery;
import cn.xizikelif.basic.service.impl.BaseServiceImpl;
import cn.xizikelif.basic.util.PageList;
import cn.xizikelif.org.aop.BaseAop;
import cn.xizikelif.org.domain.Department;
import cn.xizikelif.org.mapper.DepartmentMapper;
import cn.xizikelif.org.service.IDepartmentServiceI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class DepartmentServiceImplI extends BaseServiceImpl<Department> implements IDepartmentServiceI{
        @Autowired
        private DepartmentMapper mapper;


        @Override
        @BaseAop(name = "部门查找单个")
        public Department findById(Long id) {
                return mapper.findById(id);
        }

        @Override
        @BaseAop(name = "部门查找全部")
        public List<Department> findAll() {
                return mapper.findAll();
        }

        @Override
        @Transactional
        @BaseAop(name = "部门添加")
        public void add(Department department) {
                mapper.add(department);
        }

        @Override
        @Transactional
        @BaseAop(name = "部门修改")
        public void update(Department department) {
                mapper.update(department);
        }

        @Override
        @Transactional
        @BaseAop(name = "部门删除")
        public void delete(Long id) {
                mapper.delete(id);
        }



        @Override
        @BaseAop(name = "部门分页查询")
        public PageList<Department> queryByPage(BaseQuery query) {
                // 1.获取分页总条数
                Integer totals = mapper.queryByCount(query);
                // 2.判断分页总条数是否有有数据，没有则返回 0 或者一个 []
                if(null == totals || 0 == totals){
                        return new PageList<Department>(0,new ArrayList<>());
                }
                // 3.获取分页数据
                List<Department> rows = mapper.queryByPage(query);
                // 4.返回分页对象
                return new PageList<>(totals,rows);
        }

        @Override
        @BaseAop(name = "")
        public Integer queryByCount(BaseQuery query) {
                return mapper.queryByCount(query);
        }

        @Override
        @BaseAop(name = "部门批量删除")
        public void bachDelete(List<Long> ids) {
                mapper.bachDelete(ids);
        }
                
        

        /* 无限级数 map 玩法， 双层for循环，100*100*/
        @Override
        @BaseAop(name = "部门无限级数")
        public List<Department> tree() {
                List<Department> result = new ArrayList<>();
                // .查询所有出所有数据
                List<Department> departmentsAll = mapper.findAll();
                // 在内存中拼接·部门·
                for (Department dept :  departmentsAll) {
                        if(null == dept.getParentId()) {
                                result.add(dept);
                        }else {
                                for (Department parent : departmentsAll) {
                                        if(dept.getParentId().equals(parent.getId())){
                                                parent.getChildren().add(dept);
                                                break;
                                        }
                                }

                        }
                }
                return result;
        }

        /* 无限级数 map 玩法， for遍历100次，map遍历100次 */
      /*  @Override
        public List<Department> tree() {
                List<Department> result = new ArrayList<>();
                // .查询所有出所有数据
                List<Department> departmentsAll = mapper.findAll();
                *//*将所有数据转成 key 和 value*//*
                Map<Long, Department> departmentMap = departmentsAll.stream().collect(Collectors.toMap(Department::getId, dept -> dept));
                // 在内存中拼接·部门·
                for (Department dept :  departmentsAll) {
                        if(null == dept.getParentId()) {
                                result.add(dept);
                        }else {
                                Department parent = departmentMap.get(dept.getParentId());
                                parent.getChildren().add(dept);
                                *//*for (Department parent : departmentsAll) {
                                        if(dept.getParentId().equals(parent.getId())){
                                                parent.getChildren().add(dept);
                                                break;
                                        }*//*
                                }

                        }
                return result;
        }*/

}
